package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.Ingredient;
import com.spice.service.mapper.IngredientMapper;
import com.spice.service.service.IngredientService;
import org.springframework.stereotype.Service;

@Service
public class IngredientServiceImpl extends ServiceImpl<IngredientMapper, Ingredient> implements IngredientService {
    @Override
    public Response saveIngredient(Ingredient newIngredient) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(
                newIngredient.getSource(),
                newIngredient.getMaintainer()
        )){return Response.setResult(ResultCodeEnum.PARAM_LACK);}
        //检查名称是否重复
        if (this.count(new LambdaQueryWrapper<Ingredient>().eq(Ingredient::getSource,newIngredient.getSource())) > 0)
        {
            throw new SpiceException().message("该原料信息已存在");
        }
        this.save(newIngredient);
        return Response.ok().message("配料信息保存成功").data("data",newIngredient);
    }

    @Override
    public Response removeIngredient(Long id) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        }
        //确认待删除数据是否存在
        Ingredient removedIngredient = this.getById(id);
        if (CheckUtil.objectsHasNull(removedIngredient))
        {
            throw new SpiceException().message("该配料数据不存在");
        }
        if (!this.removeById(removedIngredient))
        {
            return Response.error().message("删除失败");
        }
        return Response.ok().message("配料信息删除成功").data("data",removedIngredient);
    }

    @Override
    public Response updateIngredient(Long id, Ingredient newIngredient) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newIngredient)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认更新的是否存在
        Ingredient updatedIngredient = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedIngredient))
        {
            throw new SpiceException().message("该配料信息不存在");
        }
        //确认更新后的配料名称是否与已有的名称重复
        //更新的名称与已有的不同的情况下
        if (!updatedIngredient.getSource().equals(newIngredient.getSource()))
        {
            if (this.count(new LambdaQueryWrapper<Ingredient>().eq(Ingredient::getSource,newIngredient.getSource())) > 0)
            {
                throw new SpiceException().message("该配料信息已存在");
            }
        }
        //更新待更新的数据id和createTime
        newIngredient.setId(id);
        newIngredient.setCreateTime(updatedIngredient.getCreateTime());
        if (this.updateById(newIngredient))
        {
            return Response.ok().message("配料信息更新成功").data("data",newIngredient);
        }
        else {
            return Response.error().message("配料信息更新失败");
        }
    }
}
